package com.northpool.operator.common.crypto;

import cn.hutool.core.util.StrUtil;
import com.northpool.operator.annotation.InputParam;
import com.northpool.operator.annotation.InputParams;
import com.northpool.operator.annotation.OperatorInfo;
import com.northpool.operator.annotation.OutputParam;
import com.northpool.operator.annotation.OutputParams;
import com.northpool.operator.common.algorithm.OperatorTestBuild;
import com.northpool.operator.common.algorithm.TestParamBuilder;
import com.northpool.operator.common.sm.SM4;
import com.northpool.operator.param.ParamTool;
import com.northpool.operator.type.TypeEnum;
import com.northpool.operator.utils.StpOperatorHelper;


@OperatorInfo(
        ref = "DecryptOperator",
        name = "解密",
        version = "sm4",
        description = "字符串解密算子"
)
@OutputParams({
        @OutputParam(ref = "result", name = "解密后的值", desc = "解密后的sm4值")
})
public class Sm4DecryptOperator extends StpOperatorHelper {

    @InputParams({
            @InputParam(ref = "str", name = "待加密字符串", desc = "待sm4加密字符串", type = TypeEnum.STRING, required = true),
            @InputParam(ref = "key", name = "密钥", desc = "密钥", type = TypeEnum.STRING, required = false, value = "")
    })
    public Sm4DecryptOperator() {
    }

    public static void main(String[] args) {
        OperatorTestBuild.execute(Sm4DecryptOperator.class, TestParamBuilder.aBuilder()
                .param("str", "qqywOvFHtzhiliiMA/v32A==")
                .param("key", "11HDESaAhiHHugDz")
                .build());
    }

    @Override
    public void execute(ParamTool paramTool) throws Exception {
        String str = paramTool.getStr("str");
        String key = paramTool.getStr("key");

        String sm4 = StrUtil.isNotBlank(key) ?
                SM4.decryptData_ECB(str, key) : SM4.decryptData_ECB(str);

        this.result("result", sm4);
    }
}
